package Market;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import Market.CBD.DataDer;
import Market.CBD.CBDMarketDataOuterClass.CBDMarketData;

public class DataConsumer {

    public static void main(String[] argv){
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "test_group");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, DataDer.class);
        KafkaConsumer<String, CBDMarketData> consumer = new KafkaConsumer<>(props);

        ConsumerRecords<String, CBDMarketData> consumerRecords;

        consumer.subscribe(Arrays.asList("stream-out"));

        while(true){
            consumerRecords = consumer.poll(Duration.ofMillis(1000));
            if(consumerRecords.count()<0)break;
            for(ConsumerRecord<String, CBDMarketData> consumerRecord : consumerRecords){
                System.out.println(consumerRecord.value());
            } 
        }

        consumer.close();
    }
}

